
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>array --- 高效的数值数组 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="weakref --- 弱引用" href="weakref.html" />
    <link rel="prev" title="bisect --- 数组二分查找算法" href="bisect.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/array.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="weakref.html" title="weakref --- 弱引用"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="bisect.html" title="bisect --- 数组二分查找算法"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-array">
<span id="array-efficient-arrays-of-numeric-values"></span><h1><a class="reference internal" href="#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> --- 高效的数值数组<a class="headerlink" href="#module-array" title="永久链接至标题">¶</a></h1>
<hr class="docutils" id="index-0" />
<p>此模块定义了一种对象类型，可以紧凑地表示基本类型值的数组：字符、整数、浮点数等。 数组属于序列类型，其行为与列表非常相似，不同之处在于其中存储的对象类型是受限的。 类型在对象创建时使用单个字符的 <em class="dfn">类型码</em> 来指定。 已定义的类型码如下：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 14%" />
<col style="width: 25%" />
<col style="width: 24%" />
<col style="width: 29%" />
<col style="width: 9%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>类型码</p></th>
<th class="head"><p>C 类型</p></th>
<th class="head"><p>Python 类型</p></th>
<th class="head"><p>以字节表示的最小尺寸</p></th>
<th class="head"><p>注释</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
<td><p>signed char</p></td>
<td><p>int</p></td>
<td><p>1</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
<td><p>unsigned char</p></td>
<td><p>int</p></td>
<td><p>1</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td>
<td><p>Py_UNICODE</p></td>
<td><p>Unicode 字符</p></td>
<td><p>2</p></td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td>
<td><p>signed short</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>
<td><p>unsigned short</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>
<td><p>signed int</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td>
<td><p>unsigned int</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'l'</span></code></p></td>
<td><p>signed long</p></td>
<td><p>int</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'L'</span></code></p></td>
<td><p>unsigned long</p></td>
<td><p>int</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'q'</span></code></p></td>
<td><p>signed long long</p></td>
<td><p>int</p></td>
<td><p>8</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></p></td>
<td><p>unsigned long long</p></td>
<td><p>int</p></td>
<td><p>8</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
<td><p>float</p></td>
<td><p>float</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
<td><p>double</p></td>
<td><p>float</p></td>
<td><p>8</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>注释:</p>
<ol class="arabic">
<li><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code> 类型码对应于 Python 中已过时的 unicode 字符 (<a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> 即 <code class="xref c c-type docutils literal notranslate"><span class="pre">wchar_t</span></code>)。 根据系统平台的不同，它可能是 16 位或 32 位。</p>
<p><code class="docutils literal notranslate"><span class="pre">'u'</span></code> 将与其它的 <a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> API 一起被移除。</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.3, will be removed in version 4.0.</span></p>
</div>
</li>
</ol>
<p>值的实际表示会由机器的架构决定（严格地说是由 C 实现决定）。 实际大小可通过 <code class="xref py py-attr docutils literal notranslate"><span class="pre">itemsize</span></code> 属性来获取。</p>
<p>这个模块定义了以下类型：</p>
<dl class="class">
<dt id="array.array">
<em class="property">class </em><code class="sig-prename descclassname">array.</code><code class="sig-name descname">array</code><span class="sig-paren">(</span><em class="sig-param">typecode</em><span class="optional">[</span>, <em class="sig-param">initializer</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array" title="永久链接至目标">¶</a></dt>
<dd><p>一个包含由 <em>typecode</em> 限制类型的条目的新数组，并由可选的 <em>initializer</em> 值进行初始化，该值必须为一个列表、<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或包含正确类型元素的可迭代对象。</p>
<p>如果给定一个列表或字符串，该 initializer 会被传给新数组的 <a class="reference internal" href="#array.array.fromlist" title="array.array.fromlist"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromlist()</span></code></a>, <a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> 或 <a class="reference internal" href="#array.array.fromunicode" title="array.array.fromunicode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromunicode()</span></code></a> 方法（见下文）以将初始条目添加到数组中。 否则会将可迭代对象作为 initializer 传给 <a class="reference internal" href="#array.array.extend" title="array.array.extend"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extend()</span></code></a> 方法。</p>
</dd></dl>

<dl class="data">
<dt id="array.typecodes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">typecodes</code><a class="headerlink" href="#array.typecodes" title="永久链接至目标">¶</a></dt>
<dd><p>一个包含所有可用类型码的字符串。</p>
</dd></dl>

<p>数组对象支持普通的序列操作如索引、切片、拼接和重复等。 当使用切片赋值时，所赋的值必须为具有相同类型码的数组对象；所有其他情况都将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。 数组对象也实现了缓冲区接口，可以用于所有支持 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节类对象</span></a> 的场合。</p>
<p>以下数据项和方法也受到支持：</p>
<dl class="attribute">
<dt id="array.array.typecode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">typecode</code><a class="headerlink" href="#array.array.typecode" title="永久链接至目标">¶</a></dt>
<dd><p>用于创建数组的类型码字符。</p>
</dd></dl>

<dl class="attribute">
<dt id="array.array.itemsize">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">itemsize</code><a class="headerlink" href="#array.array.itemsize" title="永久链接至目标">¶</a></dt>
<dd><p>在内部表示中一个数组项的字节长度。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.append">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">append</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.append" title="永久链接至目标">¶</a></dt>
<dd><p>添加一个值为 <em>x</em> 的新项到数组末尾。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.buffer_info">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">buffer_info</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.buffer_info" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个元组 <code class="docutils literal notranslate"><span class="pre">(address,</span> <span class="pre">length)</span></code> 以给出用于存放数组内容的缓冲区元素的当前内存地址和长度。 以字节表示的内存缓冲区大小可通过 <code class="docutils literal notranslate"><span class="pre">array.buffer_info()[1]</span> <span class="pre">*</span> <span class="pre">array.itemsize</span></code> 来计算。 这在使用需要内存地址的低层级（因此不够安全） I/O 接口时会很有用，例如某些 <code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> 操作。 只要数组存在并且没有应用改变长度的操作，返回数值就是有效的。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>当在 C 或 C++ 编写的代码中使用数组对象时（这是有效使用此类信息的唯一方式），使用数组对象所支持的缓冲区接口更为适宜。 此方法仅保留用作向下兼容，应避免在新代码中使用。 缓冲区接口的文档参见 <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">缓冲协议</span></a>。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="array.array.byteswap">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">byteswap</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.byteswap" title="永久链接至目标">¶</a></dt>
<dd><p>“字节对调”所有数组项。 此方法只支持大小为 1, 2, 4 或 8 字节的值；对于其他值类型将引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>。 它适用于从不同字节序机器所生成的文件中读取数据的情况。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.count">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">count</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.count" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <em>x</em> 在数组中的出现次数。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.extend">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">extend</code><span class="sig-paren">(</span><em class="sig-param">iterable</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.extend" title="永久链接至目标">¶</a></dt>
<dd><p>将来自 <em>iterable</em> 的项添加到数组末尾。 如果 <em>iterable</em> 是另一个数组，它必须具有 <em>完全</em> 相同的类型码；否则将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。 如果 <em>iterable</em> 不是一个数组，则它必须为可迭代对象并且其元素必须为可添加到数组的适当类型。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.frombytes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">frombytes</code><span class="sig-paren">(</span><em class="sig-param">s</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.frombytes" title="永久链接至目标">¶</a></dt>
<dd><p>添加来自字符串的项，将字符串解读为机器值的数组（相当于使用 <a class="reference internal" href="#array.array.fromfile" title="array.array.fromfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromfile()</span></code></a> 方法从文件中读取数据）。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><a class="reference internal" href="#array.array.fromstring" title="array.array.fromstring"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromstring()</span></code></a> 重命名为 <a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> 以使其含义更清晰。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="array.array.fromfile">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromfile</code><span class="sig-paren">(</span><em class="sig-param">f</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromfile" title="永久链接至目标">¶</a></dt>
<dd><p>从 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em> 中读取 <em>n</em> 项（解读为机器值）并将它们添加到数组末尾。 如果可读取数据少于 <em>n</em> 项则将引发 <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>，但有效的项仍然会被插入数组。 <em>f</em> 必须为一个真实的内置文件对象；不支持带有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 方法的其它对象。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.fromlist">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromlist</code><span class="sig-paren">(</span><em class="sig-param">list</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromlist" title="永久链接至目标">¶</a></dt>
<dd><p>添加来自 list 的项。 这等价于 <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">list:</span> <span class="pre">a.append(x)</span></code>，区别在于如果发生类型错误，数组将不会被改变。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.fromstring">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromstring</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromstring" title="永久链接至目标">¶</a></dt>
<dd><p>已弃用的 <a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> 的别名。</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2, will be removed in version 3.9.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="array.array.fromunicode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromunicode</code><span class="sig-paren">(</span><em class="sig-param">s</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromunicode" title="永久链接至目标">¶</a></dt>
<dd><p>使用来自给定 Unicode 字符串的数组扩展数组。 数组必须是类型为 <code class="docutils literal notranslate"><span class="pre">'u'</span></code> 的数组；否则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 请使用 <code class="docutils literal notranslate"><span class="pre">array.frombytes(unicodestring.encode(enc))</span></code> 来将 Unicode 数据添加到其他类型的数组。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.index">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">index</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.index" title="永久链接至目标">¶</a></dt>
<dd><p>返回最小的 <em>i</em> 使得 <em>i</em> 为 <em>x</em> 在数组中首次出现的序号。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.insert">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">insert</code><span class="sig-paren">(</span><em class="sig-param">i</em>, <em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.insert" title="永久链接至目标">¶</a></dt>
<dd><p>将值 <em>x</em> 作为新项插入数组的 <em>i</em> 位置之前。 负值将被视为相对于数组末尾的位置。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.pop">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">pop</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">i</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.pop" title="永久链接至目标">¶</a></dt>
<dd><p>从数组中移除序号为 <em>i</em> 的项并将其返回。 可选参数值默认为 <code class="docutils literal notranslate"><span class="pre">-1</span></code>，因此默认将移除并返回末尾项。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.remove">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">remove</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.remove" title="永久链接至目标">¶</a></dt>
<dd><p>从数组中移除首次出现的 <em>x</em>。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.reverse">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">reverse</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.reverse" title="永久链接至目标">¶</a></dt>
<dd><p>反转数组中各项的顺序。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.tobytes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tobytes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tobytes" title="永久链接至目标">¶</a></dt>
<dd><p>将数组转换为一个机器值数组并返回其字节表示（即相当与通过 <a class="reference internal" href="#array.array.tofile" title="array.array.tofile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tofile()</span></code></a> 方法写入到文件的字节序列。）</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><a class="reference internal" href="#array.array.tostring" title="array.array.tostring"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tostring()</span></code></a> 被重命名为 <a class="reference internal" href="#array.array.tobytes" title="array.array.tobytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tobytes()</span></code></a> 以使其含义更清晰。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="array.array.tofile">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tofile</code><span class="sig-paren">(</span><em class="sig-param">f</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tofile" title="永久链接至目标">¶</a></dt>
<dd><p>将所有项（作为机器值）写入到 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em>。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.tolist">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tolist</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tolist" title="永久链接至目标">¶</a></dt>
<dd><p>将数组转换为包含相同项的普通列表。</p>
</dd></dl>

<dl class="method">
<dt id="array.array.tostring">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tostring</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tostring" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#array.array.tobytes" title="array.array.tobytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tobytes()</span></code></a> 的已弃用别名。</p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 3.2, will be removed in version 3.9.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="array.array.tounicode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tounicode</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tounicode" title="永久链接至目标">¶</a></dt>
<dd><p>将数组转换为一个 Unicode 字符串。 数组必须是类型为 <code class="docutils literal notranslate"><span class="pre">'u'</span></code> 的数组；否则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。 请使用 <code class="docutils literal notranslate"><span class="pre">array.tobytes().decode(enc)</span></code> 来从其他类型的数组生成 Unicode 字符串。</p>
</dd></dl>

<p>当一个数组对象被打印或转换为字符串时，它会表示为 <code class="docutils literal notranslate"><span class="pre">array(typecode,</span> <span class="pre">initializer)</span></code>。 如果数组为空则 <em>initializer</em> 会被省略，否则如果 <em>typecode</em> 为 <code class="docutils literal notranslate"><span class="pre">'u'</span></code> 则它是一个字符串，否则它是一个数字列表。 使用 <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 保证能将字符串转换回具有相同类型和值的数组，只要 <a class="reference internal" href="#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array</span></code></a> 类已通过 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">array</span> <span class="pre">import</span> <span class="pre">array</span></code> 被引入。 例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;u&#39;</span><span class="p">,</span> <span class="s1">&#39;hello </span><span class="se">\u2641</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.14</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>模块 <a class="reference internal" href="struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a></dt><dd><p>打包和解包异构二进制数据。</p>
</dd>
<dt>模块 <a class="reference internal" href="xdrlib.html#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xdrlib</span></code></a></dt><dd><p>打包和解包用于某些远程过程调用系统的 External Data Representation (XDR) 数据。</p>
</dd>
<dt><a class="reference external" href="https://docs.scipy.org/doc/">The Numerical Python Documentation</a></dt><dd><p>Numeric Python 扩展 (NumPy) 定义了另一种数组类型；请访问 <a class="reference external" href="http://www.numpy.org/">http://www.numpy.org/</a> 了解有关 Numerical Python 的更多信息。</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="bisect.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code> --- 数组二分查找算法</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="weakref.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> --- 弱引用</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/array.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="weakref.html" title="weakref --- 弱引用"
             >下一页</a> |</li>
        <li class="right" >
          <a href="bisect.html" title="bisect --- 数组二分查找算法"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>